package com.example.certificate.utils;

import com.alibaba.fastjson.JSONObject;
import com.example.certificate.entity.Meta;
import com.example.certificate.entity.SysResource;
import com.example.certificate.entity.SysRole;
import com.example.certificate.entity.Tree;
import com.example.certificate.mapper.SysRouterAuthMapper;
import org.springframework.util.CollectionUtils;


import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class NodeTreeUtil {
    /**
     * list数据转换为tree数据
     * 根据parentId把list数据转为map分组
     * list循环数据根据id查询分组 把对应的map分组数据插入到对应的children
     * @param zoneList
     * @return
     */
    public static List<SysResource> nodeTress(List<SysResource> zoneList) {
        List<SysResource> list = new ArrayList<>();
        zoneList.forEach(item ->{
            if(item != null){
                list.add(item);
            }
        });
        zoneList = list;
        Map<String, List<SysResource>> zoneByParentIdMap = zoneList.stream().collect(Collectors.groupingBy(SysResource::getParentId));
        zoneList.forEach(zone -> {
            zone.setMeta(JSONObject.parseObject(zone.getMetaDesc(), Meta.class));
            zone.setChildren(zoneByParentIdMap.get(zone.getId()));
        });
        return zoneList.stream().filter(v -> "0".equals(String.valueOf(v.getParentId()))).collect(Collectors.toList());
    }


    public static List<Tree> getResourceTree(List<Tree> resources, Boolean isTrue, SysRouterAuthMapper authMapper){
        Map<String, List<Tree>> parentGroup = resources.stream()
                .sorted(Comparator.comparing(Tree::getSort))
                .collect(Collectors.groupingBy(Tree::getParentId));
        if(isTrue){
            resources.stream().forEach(item ->{
                item.setChildren(parentGroup.get(item.getId()));
            });
        }else {
            resources.stream().forEach(item ->{
                if(CollectionUtils.isEmpty(parentGroup.get(item.getId()))){
                    item.setChildren(authMapper.roleAuthTree(item.getId()));
                }else {
                    item.setChildren(parentGroup.get(item.getId()));
                }
            });
        }
        return resources.stream().filter(v -> "0".equals(v.getParentId()))
                .sorted(Comparator.comparing(s -> String.valueOf(s.getSort())))
                .collect(Collectors.toList());
    }

    /**
     * @Author Hao
     * @Description 树形控件
     * @Date 2021/12/17
     * @Param [zoneList]
     * @return java.util.List<com.han.nternational.domain.entity.SysRole>
     **/
    public static List<SysRole> sysRolesTress(List<SysRole> zoneList) {
        Map<String, List<SysRole>> zoneByParentIdMap = zoneList.stream().collect(Collectors.groupingBy(SysRole::getParentId));
        zoneList.forEach(zone -> zone.setChildren(zoneByParentIdMap.get(zone.getId())));
        return zoneList.stream()
                .filter(v -> "0".equals(v.getParentId()))
                .sorted(Comparator.comparing(s -> String.valueOf(s.getSort())))
                .collect(Collectors.toList());
    }
}